package guava.range;

import com.google.common.collect.BoundType;
import com.google.common.collect.DiscreteDomain;
import com.google.common.collect.Range;

import java.util.Arrays;
import java.util.List;

/**
 * Created by ZhuQianli on 2018/1/26.
 */
public class App {
    public static void main(String[] args) {

        // open 全开   closed 全闭    openClosed 左开有闭   closedOpen 左闭右开
        Range<Integer> integerRange = Range.closedOpen(1, 20);
        System.out.println(integerRange.contains(0));
        System.out.println(integerRange.contains(1));
        System.out.println(integerRange.contains(10));
        System.out.println(integerRange.contains(20));
        System.out.println(integerRange.contains(21));


        // >=10
        Range<Integer> integerRange1 = Range.atLeast(10);
        System.out.println(integerRange1.contains(10)); // T
        // >10
        Range<Integer> integerRange2 = Range.greaterThan(10);
        System.out.println(integerRange2.contains(10)); //F
        // <=10
        Range<Integer> integerRange3 = Range.atMost(10);
        System.out.println(integerRange3.contains(10)); //T
        // >10
        Range<Integer> integerRange4 = Range.lessThan(10);
        System.out.println(integerRange4.contains(10)); //F
        // 包含参数 !!! 获取参数里的最大最小数,然后取全包 !!!
        List<Integer> integers = Arrays.asList(1, 10, 18);
        Range<Integer> integerRange5 = Range.encloseAll(integers);
        System.out.println(integerRange5.contains(1)); //T
        System.out.println(integerRange5.contains(10)); //T
        System.out.println(integerRange5.contains(18)); //T
        System.out.println(integerRange5.contains(5)); //T
        // 所有
        Range<Comparable<?>> all = Range.all();
        System.out.println(all.contains(123));//T
        // close >=10    open >10     下降到10
        Range<Integer> integerRange6 = Range.downTo(10, BoundType.CLOSED);
        System.out.println(integerRange6.contains(10));//T
        System.out.println(integerRange6.contains(11));//T
        Range<Integer> integerRange7 = Range.downTo(10, BoundType.OPEN);
        System.out.println(integerRange7.contains(10));//F
        // 上升到10
        Range<Integer> integerRange8 = Range.upTo(10, BoundType.CLOSED);
        System.out.println(integerRange8.contains(-1));//T
        // ==10
        Range<Integer> singleton = Range.singleton(10);
        System.out.println(singleton.contains(9));//f
        System.out.println(singleton.contains(10));//t
        System.out.println(singleton.contains(11));//f
        // 0<x<=10
        Range<Integer> range = Range.range(0, BoundType.OPEN, 10, BoundType.CLOSED);
        System.out.println(range.contains(-11));//f
        System.out.println(range.contains(0));//f
        System.out.println(range.contains(3));//t
        System.out.println(range.contains(10));//t
        System.out.println(range.contains(11));//f

        // canonical规范range    就是变成左闭右开的形式     (0..10] ----> [1..11)   (0..10) ----> [1..10]
        Range<Integer> range1 = Range.range(0, BoundType.OPEN, 10, BoundType.OPEN);
        Range<Integer> canonical = range1.canonical(DiscreteDomain.integers());
        System.out.println(range1+"  " + canonical); // (0..10)  [1..10)

        // 取交集
        Range<Integer> open = Range.open(1, 10);
        Range<Integer> open1 = Range.open(2, 4);
        System.out.println(open.intersection(open1));//(2..4)
        System.out.println(open1.intersection(open));//(2..4)
        Range<Integer> open2 = Range.closedOpen(2, 4);
        System.out.println(open2.intersection(open));//[2..4)
        Range<Integer> open3 = Range.open(-100, 123);
        System.out.println(open3.intersection(open));//(1..10)
        Range<Integer> open4 = Range.open(4, 123);
        System.out.println(open4.intersection(open));//(4..10)
        Range<Integer> open5 = Range.open(123, 312);
        System.out.println(open5.intersection(open));


    }

}
